//#define __DEBUG__
// by Tyrghen
#define THIS_FILE "fn_scriptedreactions.sqf"
#include "x_setup.sqf"

if (typeName _this == typeName []) then {
	{ _x call FUNC(scriptedreactions);
	} foreach _this;
} else {
	if (typeName _this == typeName objNull && {!(isNull _this)}) then {
		_this addEventHandler ["Hit",{
			// Only reacts when damage comes from a player, otherwise it could be a stupid AI vehicle
			if (isPlayer (_this select 1)) then {
				private ["_inc"];
				__TRACE_1("EH Hit","_this")
				(group (_this select 0)) setCombatMode "RED";
				(group (_this select 0)) setSpeedMode "FULL";
				(leader group (_this select 0)) setSpeedMode "FULL";
				if ((behaviour (leader group (_this select 0))) in ["SAFE","AWARE"]) then { (group (_this select 0)) setBehaviour "COMBAT"; };
				_inc = ((group (_this select 0)) knowsAbout (_this select 1)) + 0.1;
				if (_inc < 2) then { _inc = _inc + 0.1; };
				(group (_this select 0)) reveal [(_this select 1), _inc];
				__TRACE_1("Knows about","((group (_this select 0)) knowsAbout (_this select 1))")
			};
		}];
		_this addEventHandler ["Killed",{
			// Only reacts when damage comes from a player, otherwise it could be a stupid AI vehicle
			if (isPlayer (_this select 1)) then {
				__TRACE_1("EH Killed","_this")
				(group (_this select 0)) setCombatMode "RED";
				(group (_this select 0)) setSpeedMode "FULL";
				(leader group (_this select 0)) setSpeedMode "FULL";
				if ((behaviour (leader group (_this select 0))) in ["SAFE","AWARE"]) then {
					(group (_this select 0)) setBehaviour "COMBAT";
				};
				// If the ASR_AI is present, lets use it...
				if (isNil "asr_ai3_main_fnc_moveToCover") then {
					__TRACE_2("Group test cover","(group (_this select 0))","((group (_this select 0)) getVariable 'd_REACTION')")
					if !((group (_this select 0)) getVariable [QGVAR(REACTION),false]) then {
						(group (_this select 0)) spawn {
							// TYRGHEN - Ultra basic move to cover function, not meant to be an almighty function, but enough to get some reactions.
							private ["_objs","_out","_leader"];
							_this setVariable [QGVAR(REACTION),true];
							_this lockWp true;
							_this setCombatMode "RED";
							_this setSpeedMode "FULL";
							_this setBehaviour "COMBAT";
							_objs = nearestObjects [(leader _this), ["Car","Truck","HouseBase","BagFence_base_F","BagBunker_base_F"], 30];
							__TRACE_2("Group move to cover","_this","_objs")
							_out = time + 30;
							waituntil {sleep 1.345; isNull _this || (_out < time) || {alive (leader _this)}};
							_leader = (leader _this);
							if (!(isNull _this)) then {
								if (count _objs > 0) then {
									doStop (leader _this);
									_this setVariable [QGVAR(REACTION_POS), (position (_objs select 0))];
									(leader _this) doMove (_this getVariable QGVAR(REACTION_POS));
									(leader _this) setSpeedMode "FULL";
								} else {
									doStop (leader _this);
									_this setVariable [QGVAR(REACTION_POS), ([position (leader _this), 80] call FUNC(GetRanPointCircle))];
									(leader _this) doMove (_this getVariable QGVAR(REACTION_POS));
									(leader _this) setSpeedMode "FULL";
									_this setCombatMode "RED";
									_this setBehaviour "STEALTH";
								};
								{ if (_x != leader _x) then { _x doFollow (leader _x) };							
								} foreach (units _this);
								_this setSpeedMode "FULL";
								__TRACE_2("Group started moving","_this","units _this")
							};
							sleep (15 + random 10);
							if (!(isNull _this) && {!(alive _leader) && (alive (leader _this))}) then {
								(leader _this) doMove (_this getVariable QGVAR(REACTION_POS));
								(leader _this) setSpeedMode "FULL";
								_this setCombatMode "RED";
								_this setBehaviour "COMBAT";
							};
							sleep (40 + random 40);
							if !(isNull _this) then {
								_this lockwp false;
								_this setVariable [QGVAR(REACTION),false];
								(units _this) joinSilent _this;
							};
						};
					};
				} else {
					__TRACE_1("ASR AI move to cover","(group (_this select 0))")
					group (_this select 0) setSpeedMode "FULL";
					leader (_this select 0) setSpeedMode "FULL";
					[leader (_this select 0),(_this select 1),diag_ticktime,30] call asr_ai3_main_fnc_moveToCover;
				};
				_inc = ((group (_this select 0)) knowsAbout (_this select 1)) + 0.1;
				if (_inc < 2) then { _inc = _inc + 0.1; };
				(group (_this select 0)) reveal [(_this select 1), _inc];
				__TRACE_1("Knows about","((group (_this select 0)) knowsAbout (_this select 1))")
			};			
		}];
		_this addEventHandler ["FiredNear",{
			__TRACE_1("EH FiredNear","_this")
			if (leader (_this select 0) == (_this select 0)) then {
				if ((behaviour (leader group (_this select 0))) in ["SAFE","AWARE"]) then { (group (_this select 0)) setBehaviour "COMBAT"; };
			};
		}];
	};
}